<?php
/**
 * Created by PhpStorm.
 * User: 游贤川
 * Date: 2021/7/30
 * Time: 22:02
 */

namespace App\Http\Controllers;


use App\Models\RcUser;
use App\Models\UserDou;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class RcUserController extends Controller
{


    public function addUser(Request $request)
    {

        //验证
        $verify = [
            'openId'=>'required',
            'productCode'=>'required',
            'douPrice'=>'required'
        ];
        $regulation = [
            'openId.required' => 'openId必填',
            'productCode.required' => 'productCode必填',
            'douPrice.required' => 'douPrice必填',
        ];
        $validator = Validator::make($request->all(), $verify, $regulation);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        try{
            DB::beginTransaction();
            $data = $request->input();
            $model1 = $this->newUser($data);
            if ($model1->code != 200){
                DB::commit();
                return $this->error($model1->msg);
            }
            UserDou::query()->create(['openId'=>$data['openId'],'douPrice'=>$data['douPrice']]);
            //换卡
            $app_data = $this->appUser($data);
            if ($app_data->code != 0){
                DB::commit();
                return $this->error($app_data->msg);
            }
            $user = RcUser::query()->where('openId',$data['openId'])->updateOrCreate(['cardType'=>$app_data->data->cardType],['openId'=>$data['openId'],'idCard'=>$app_data->data->idCard,'startDate'=>$app_data->data->startDate,'expireDate'=>$app_data->data->expireDate,'cardType'=>$app_data->data->cardType]);
            DB::commit();
            return $this->success($user,'成功');
        }catch (\Exception $exception){
            DB::rollBack();
            Log::error('扣豆错误',[$exception->getMessage()]);
            return $this->error($exception->getMessage());
        }

    }


    public function newUser($params)
    {
        $client = new Client(['base_uri' => 'https://wangcai.abcdefg.fun/wangwangdou/api/award-list/consume_interest', 'timeout' => 5.0,]);
        $response = $client->post('', ['json' => $params]);
        $data = $response->getBody()->getContents();
       return json_decode($data);

    }

    public function appUser($params)
    {
        $client = new Client(['base_uri' => 'https://app.51ljb.cn/api/v2/channel/pingan/wangcai/generate_member', 'timeout' => 5.0,]);
        $response = $client->post('', ['json' => $params]);
        $data = $response->getBody()->getContents();
        return json_decode($data);
    }

    public function user(Request $request)
    {
        //验证
        $verify = [
            'openId'=>'required',
            'cardType'=>'required',
        ];
        $regulation = [
            'openId.required' => 'openId必填',
            'cardType.required' => 'cardType必填',
        ];
        $validator = Validator::make($request->all(), $verify, $regulation);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $data = $request->input();
        $user = RcUser::query()->where(['openId'=>$data['openId'],'cardType'=>$data['cardType']])->first();
//        $response = [
//            'cardType1'=>$user->where('cardType',1)->first(),
//            'cardType2'=>$user->where('cardType',2)->first()
//        ];
        return  $this->success($user,'成功');
    }

}
